package com.ashin;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 */
public class TestRedis {

    @Test
    public void test() {
        Jedis jedis = new Jedis("localhost");
        jedis.set("foo", "bar");
        String value = jedis.get("foo");
        System.out.println(value);
    }

    @Test
    public void testPub() {
        Jedis jedis = new Jedis("localhost");

        new Random().ints().limit(100).forEach((i) -> {
            jedis.publish("zjh", "hello redis pubsub" + i);
        });
    }

    @Test
    public void testSub() {
        Jedis jedis = new Jedis("localhost");
        jedis.subscribe(new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("recevie message:" + message);
            }

        }, "zjh");

        System.out.println("exit sub...");

    }

    @Test
    public void testPSub() {
        Jedis jedis = new Jedis("localhost");
        jedis.subscribe(new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println("recevie message:" + message);
            }

        }, "zjh");

    }


}
